﻿using QYmall.Core.Common.Emum;
using QYmall.Core.Common.Extensions;
using QYmall.Core.Common.Helper;
using QYmall.Core.DBFactory.Database;
using QYmall.Core.IServices;
using QYmall.Core.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


namespace QYmall.Core.Services
{
    /// <summary>
    /// 积分兑换记录接口实现
    /// </summary>
    public class ShopConvertHistoryService : BaseService, IShopConvertHistoryService
    {
        public ShopConvertHistoryService(IDbContextFactory contentFactory) : base(contentFactory) { }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        public async Task<ShopConvertHistory?> QueryAsync(Expression<Func<ShopConvertHistory, bool>> funcWhere,
            WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            var result = await _context.Set<ShopConvertHistory>()
                .Include(x => x.ShopConvert)
                .Include(x => x.Order)
                .FirstOrDefaultAsync(funcWhere);
            return result;
        }

        /// <summary>
        /// 查询分页列表
        /// </summary>
        public async Task<PaginationList<ShopConvertHistory>> QueryPageAsync(int pageSize, int pageIndex,
            Expression<Func<ShopConvertHistory, bool>> funcWhere, string orderBy, WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            var result = _context.Set<ShopConvertHistory>()
                .Include(x => x.ShopConvert)
                .Include(x => x.Order)
                .Where(funcWhere).OrderByBatch(orderBy);//调用Linq扩展类排序
            return await PaginationList<ShopConvertHistory>.CreateAsync(pageIndex, pageSize, result);
        }
    }
}
